import mysql from 'mysql';
import config from 'config'
const pool = mysql.createPool(config.get('mysql')); //创建连接池
const getConnect = ()=>{
  return new Promise((resolve,reject)=>{
    pool.getConnection((err,connection)=>{
      if(err) reject(err);
      console.log('连接了')
      resolve(connection)
    })
  })
}
const release = (connection)=>{
  return new Promise((resolve, reject) => {
    if(!connection){
      console.log('没有连接')
      reject(false)
    }
    connection.release();
    console.log('断了连接了')
    resolve(true);
  });
}
const query = (connection,sql,...params)=>{
  return new Promise((resolve, reject)=>{
    console.log(mysql.format(sql,params));
    connection.query(sql,params,function(err,res){
      if(err){
        reject(err);
      }
      resolve(res);
    })
  })
}
export default {
  getConnect,
  release,
  query
}